﻿using Biz.Buffer;
using Biz.EF.Platform.SG;
using Hjt212Parser;
using log4net;
using System;
using System.Linq;

namespace Biz.Process
{
    public static class Processor2011ToMonitor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Processor2011ToMonitor));
        public static void Process(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrWhiteSpace(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var port = CommonInformation.GetLetportByMn(mn);
                if (port == null) return;

                using (var pCtx = new PlatformXpfContext())
                {
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes == null) return;

                    var masterMonitor = pCtx.主实时监控
                        .Where(t => t.企业基本信息ID == port.企业基本信息ID)
                        .FirstOrDefault(t => t.企业排口ID == port.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new 主实时监控();
                        masterMonitor.企业基本信息ID = port.企业基本信息ID;
                        masterMonitor.企业排口ID = port.ID;
                        pCtx.主实时监控.Add(masterMonitor);
                    }
                    if (codes.Contains("B01") || codes.Contains("w00000"))
                    {
                        masterMonitor.污染物类别ID = 2;
                        var rtd = Parser.GetDoubleByKey("B01-Rtd", content);
                        if (!rtd.HasValue) Parser.GetDoubleByKey("w00000-Rtd", content);
                        masterMonitor.瞬时量 = rtd;
                    }
                    else if (codes.Contains("B02") || codes.Contains("a00000"))
                    {
                        masterMonitor.污染物类别ID = 1;
                        var rtd = Parser.GetDoubleByKey("B02-Rtd", content);
                        if (!rtd.HasValue) Parser.GetDoubleByKey("a00000-Rtd", content);
                        masterMonitor.瞬时量 = rtd;
                    }
                    masterMonitor.实时数据时间 = datatime;
                    masterMonitor.在线状态ID = 1201;
                    masterMonitor.在线状态时间 = DateTime.Now;
                    pCtx.SaveChanges();

                    Helper.RemoveMasterFactor(codes, true);

                    foreach (var code in codes)
                    {
                        var contam = CommonInformation.GetContamCode(code);
                        if (contam == null) continue;

                        var detailMonitor = masterMonitor.从实时监控.FirstOrDefault(t => t.污染物编码ID == contam.ID);
                        if (detailMonitor == null)
                        {
                            detailMonitor = new 从实时监控();
                            //detailMonitor.主实时监控ID = masterMonitor.ID;
                            detailMonitor.污染物编码ID = contam.ID;
                            masterMonitor.从实时监控.Add(detailMonitor);
                        }
                        var v = Parser.GetDoubleByKey(code + "-Rtd", content);
                        detailMonitor.瞬时量 = v ?? 0;
                    }
                    pCtx.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                Log.Error(content);
            }
        }
    }
}
